<?php

// Namespace

namespace Loop;


/**
 * 
 */
 
class CMySqlIDriver_Test extends \UnitTestCase
{
	public function testTransaction()
	{
		// Dependencies

		require_once(PATH_LOOP . 'Class/CMySqlIDriver.php');


		// Set the preference 

		pref('[Loop]/database/activeTransaction', true);


		// Get the databaseDriver

		$databaseDriver = app()->getDatabaseDriver(EXTENSION_APPLICATION);


		// Start the transaction

		$databaseDriver->startTransaction();


		// Construct the query

		$query = "INSERT INTO `%{databaseName}`.`%{tableName}` (`name`)
					VALUES ('test1'),('test2'),('test3')";

	
		$queryVariables = array 
		(
			'databaseName' => pref('[' . EXTENSION_APPLICATION . ']/database/name'),
			'tableName' => 'loop_testobjecta'
		);


		$databaseDriver->executeQuery($query, $queryVariables);


		// Create a new connection

		$host = pref('[' . EXTENSION_APPLICATION . ']/database/host');
		$login = pref('[' . EXTENSION_APPLICATION . ']/database/login');
		$password = pref('[' . EXTENSION_APPLICATION . ']/database/password');
		$name = pref('[' . EXTENSION_APPLICATION. ']/database/name');
		$port = pref('[' . EXTENSION_APPLICATION. ']/database/port');
		$socket = pref('[' . EXTENSION_APPLICATION . ']/database/socket');
		


		// First, connect to the server and store the handle
		
		$connection = mysqli_connect
		(
			$host,
			$login,
			$password,
			'',
			$port,
			$socket
		);


		$querySelect = " SELECT * FROM `" . pref('[Engage-Reward]/database/name') . "`.`loop_testobjecta`";


		// Execute the query

		$result = mysqli_query($connection, $querySelect);


		while($data = mysqli_fetch_assoc($result)) 
	    { 
	    	$this->fail('Datas found');
	    } 

	    $this->pass();


	    // Close transaction

	    $databaseDriver->closeTransaction();


		// Execute the query

		$result = mysqli_query($connection, $querySelect);


		//

		$nbResults = 0;

		while($data = mysqli_fetch_assoc($result)) 
	    { 
	    	$nbResults ++;
	    }	

	    $this->assertEqual($nbResults, 3);
	}
}
?>